// 
//  antflviui.js
//  firefox
//  
//  Created by Zak on 2008-06-16.
//  Copyright 2008 Ant.com. All rights reserved.
// 

top.AntFlvUi = 
{
    listShown: false,
    doc: null,
    listBox: null,
      
    /**
     * Initialize the class with necessary pointers
     * @param doc           The browser interface
     */
    init: function (doc)
    {
        this.doc = doc;
        this.listBox = $('antFlvListBox', doc);
    },

	/**
	 * True if the list is shown, false if not
	 */
	isShown: function ()
	{
		return this.listShown;
	},
    
    /**
     * Toggle the bottom panel
	 * @param show optionnal when true force showing the panel when false force closing...
     */
    togglePanel: function (/* optional disable button */)
    {
        var splitter = $('antFlvListSplitter', this.doc);
        var content = $('antFlvListContent', this.doc);
        
		if (arguments.length > 0)
			this.listShown = !arguments[0];
        if (this.listShown)
        {
            splitter.setAttribute('collapsed', true);
            content.setAttribute('collapsed', true);
            this.listShown = false;
        }
        else
        {
            content.setAttribute('collapsed', false);
            splitter.setAttribute('collapsed', false);
            this.listShown = true;
        }
	},

    /**
     * Toggle the bottom panel content
	 * @param type force the type ex "list", "settings"
     */
	setContent: function (type)
	{
        var enabledBox = $('antFlvBoxEnabled', this.doc);
        var disabledBox = $('antFlvBoxDisabled', this.doc);

		if (type == "list")
		{
            enabledBox.setAttribute('collapsed', false);
            disabledBox.setAttribute('collapsed', true);
		}
		else if (type == "settings")
		{
            enabledBox.setAttribute('collapsed', true);
            disabledBox.setAttribute('collapsed', false);
		}
		else
			throw ("AntflvUi.toggleContent type argument invalid (" + type + ")");
    },


	/**
	 * set Disable property of movie list button
	 */
	disableMovieListButton: function (enable)
	{
		$("antToolBarMovieListButton").setAttribute("disabled", !enable);	
	},

	/**
	 * set Hidden property of movie list button
	 */
	hideMovieListButton: function (hide)
	{
		$("antToolBarMovieListButton").setAttribute("hidden", hide);	
		if (hide == true)
		    this.togglePanel(false);
	},
    
    /**
     * Add a ListItem to the ListBox
     * @param origin        Origin of the flv
     * @param url           Url of the flv
     * @param name          Guessed name of the flv
     * @param id            A unique ID for the listItem
     */
    appendItem: function (flvlink, id)
    {
        var listItem = this.doc.createElement('listitem');
        var listCellOrigin = this.doc.createElement('listcell');
        var listCellUrl = this.doc.createElement('listcell');
        var listCellName = this.doc.createElement('listcell');

    	listItem.setAttribute('id', 'antFlvListItem-'+id);
    	listItem.setAttribute('ondblclick', 'AntFlvList.onItemSelected(event);');
    	listItem.setAttribute('context', "antFlvListPopupMenu");

    	listCellOrigin.setAttribute('id', 'antFlvListCellOrigin-'+id);
    	listCellOrigin.setAttribute('label', flvlink.origin);

    	listCellUrl.setAttribute('id', 'antFlvListCellUrl-'+id);
    	listCellUrl.setAttribute('label', flvlink.url);
    	listCellUrl.setAttribute('hidden', true);

    	listCellName.setAttribute('id', 'antFlvListCellName-'+id);
        listCellName.setAttribute('label', flvlink.name.replace(/_/g,' '));

    	listItem.appendChild(listCellOrigin);
    	listItem.appendChild(listCellUrl);
    	listItem.appendChild(listCellName);
		listItem.downloaded = false;

    	this.listBox.insertBefore(listItem, this.listBox.childNodes[2]);

		return listItem;
    },
    
    /**
     * Remove all listItems from the ListBox
     */
    removeAll: function ()
    {
			while (this.listBox.childNodes.length > 2)
				this.listBox.removeChild(this.listBox.childNodes[2]);
    },

    /**
     * Find a node using his URL
     * @param url           The URL to match
     * @return item         a XulNode corresponding to a XUL:ListItem
     */
    getItemByUrl: function (url)
    {
        for (var i = 0; i < this.listBox.childNodes.length; i++)
		{
			var item = this.listBox.childNodes[i];

			if (url == item.childNodes[1].getAttribute("label"))
				return item;
		}
		return null;
    },

	/**
	 * Set the content of the bottom panel according to the URL (Expert Mode)
	 * @param url           The current URL
	 */
	updateContent: function (url)
    {
	   if (AntPrefs.isSiteEnabled(url))
		   this.setContent("list");
	   else
		   this.setContent("settings");
    },

	/**
	 * Disable or enable the Download button according to the content of the page
	 * (if we find our tainted flvLink item in the page or not)
	 */
	updateDownloadButton: function ()
	{
		var gBrowser = AntLib.getMainWindow().getBrowser();
		var doc = gBrowser.selectedBrowser.contentDocument;

		if (doc.__antflv__)
			$("antToolBarDownloadButton").disabled = false;
		else
			$("antToolBarDownloadButton").disabled = true;
	}
}
